import numpy as np
import matplotlib.pyplot as plt


# 定义piecewise linear函数
def piecewise_linear(x, breakpoints, slopes):
    return np.piecewise(x, [x < breakpoints[0], (x >= breakpoints[0]) & (x < breakpoints[1]), x >= breakpoints[1]],
                        [lambda x: slopes[0] * x,
                         lambda x: slopes[1] * (x - breakpoints[0]) + slopes[0] * breakpoints[0],
                         lambda x: slopes[2] * (x - breakpoints[1]) + slopes[1] * (breakpoints[1] - breakpoints[0]) +
                                   slopes[0] * breakpoints[0]])


# 图像反转
def reverse_function(x):
    return -x + 255


def log_transform(x, c=10):
    return c * np.log1p(np.abs(x))


# 定义分段函数的分段点和斜率
breakpoints = [100, 150]  # 分段点
slopes = [0.5, 2, 1]  # 斜率

# 生成x轴数据
x = np.arange(0, 256)

# 计算对应的y轴数据
# y = reverse_function(x)
y = log_transform(x, c=1)

# 绘制piecewise linear函数
plt.plot(x, y, label='Log Function', color='b')

# 设置标题和标签
plt.title('Log Function, c=1')
plt.xlabel('f(x,y)')
plt.ylabel('g(x,y)')

# 显示网格和图例
plt.grid(True)
# plt.legend()

# 显示图形
plt.show()
